﻿using Ribbon.Core.Event;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Graphic = Ribbon.Core.Graphics.GraphicHelper;

namespace Ribbon.Core
{
    public class RibbonCheckBoxItem : IRibbonItem
    {
        public RibbonCheckBoxItem(string name)
        {
            this.Name = name;

            this.Text = name;

            this.foreColor = Color.Black;

            this.backColor = Color.FromArgb(238, 238, 242);

            this.borderColor = Color.FromArgb(209, 209, 209);

            this.selectBackColor = Color.FromArgb(209, 209, 209);

            this.radiu = 0;
        }

        RibbonGroup parent;

        public RibbonGroup Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;

                if (parent != null)
                {
                    Resize();
                }
            }
        }

        bool isMouseHover;

        public bool IsMouseHover
        {
            get
            {
                return isMouseHover;
            }
            set
            {
                isMouseHover = value;
            }
        }

        private bool maxImage;

        public bool MaxImage
        {
            get { return maxImage; }
            set { maxImage = value; }
        }

        /// <summary>
        /// 边界框
        /// </summary>
        private Rect rect;

        public Rect Rect
        {
            get { return rect; }
            set { rect = value; }
        }

        /// <summary>
        /// 坐标
        /// </summary>
        private PointF location;

        public PointF Location
        {
            get { return location; }
            set { location = value; }
        }

        /// <summary>
        /// 大小
        /// </summary>
        private SizeF size;

        public SizeF Size
        {
            get { return size; }
            set { size = value; }
        }

        /// <summary>
        /// 文本
        /// </summary>
        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        private Font font;

        /// <summary>
        /// 字体
        /// </summary>
        public Font Font
        {
            get { return font; }
            set { font = value; }
        }

        /// <summary>
        ///
        /// </summary>
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private Bitmap image;

        public Bitmap Image
        {
            get { return image; }
            set { image = value; }
        }

        private int borderSize;

        public int BorderSize
        {
            get { return borderSize; }
            set { borderSize = value; }
        }

        private Color borderColor;

        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; }
        }

        private Color backColor;

        public Color BackColor
        {
            get { return backColor; }
            set { backColor = value; }
        }

        private Color selectBackColor;

        public Color SelectBackColor
        {
            get { return selectBackColor; }
            set { selectBackColor = value; }
        }

        private Color foreColor;

        public Color ForeColor
        {
            get { return foreColor; }
            set { foreColor = value; }
        }

        private float radiu;

        public float Radiu
        {
            get { return radiu; }
            set { radiu = value; }
        }

        private bool check;

        public bool Checked
        {
            get { return check; }
            set
            {
                if (CheckedChanging != null)
                {
                    CheckedChanging(this, new EventArgs());
                }

                check = value;

                if (CheckedChanged != null)
                {
                    CheckedChanged(this, new EventArgs());
                }
            }
        }

        public event MouseClickDelegate MouseClick;

        public event MouseEnterDelegate MouseEnter;

        public event MouseMoveDelegate MouseMove;

        public event MouseLeaveDelegate MouseLeave;

        public event CheckedChangedDelegate CheckedChanged;

        public event CheckedChangingDelegate CheckedChanging;

        System.Drawing.Size checkSize = System.Drawing.Size.Empty;
         
        public void Resize()
        {
            if (this.Font == null)
                this.Font = parent.Font;

            checkSize = CheckBoxRenderer.GetGlyphSize(Graphic.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);

            if (this.text != null && this.text.Length > 0)
            {
                this.size = Graphic.Graphics.MeasureString(this.text, this.font);
            }
            else
            {
                this.size = new SizeF(checkSize.Width, checkSize.Height);
            }

            if (this.size.Height > 1)
            {
                this.Rect = new Rect(parent.GroupRect.X + this.location.X, parent.GroupRect.Y + this.location.Y, checkSize.Width + this.Size.Width + 5, this.Size.Height + 5);
            }
            else
            {
                this.Rect = new Rect(parent.GroupRect.X + this.location.X, parent.GroupRect.Y + this.location.Y, checkSize.Width + this.Size.Width + 5, checkSize.Height + 5);
            }
            if (parent.GroupRect.X + parent.GroupRect.Width < this.Rect.X + this.Rect.Width + 5f)
            {
                parent.GroupRect.Width += (this.Rect.X + this.Rect.Width + 5f) - (parent.GroupRect.X + parent.GroupRect.Width);
            }
        }

        public void OnPaint(System.Drawing.Graphics g)
        {
            if (this.isMouseHover)
            {
                using (SolidBrush bru_Background = new SolidBrush(selectBackColor))
                {
                    if (this.radiu > 1)
                    {
                        using (System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath())
                        {
                            path.AddLine(this.rect.X, this.rect.Y + this.rect.Height - this.radiu, this.rect.X, this.rect.Y + this.radiu);

                            path.AddArc(this.rect.X, this.rect.Y, this.radiu, this.radiu, 180, 90);

                            path.AddLine(this.rect.X + this.radiu, this.rect.Y, this.rect.X + this.rect.Width - this.radiu, this.rect.Y);

                            path.AddArc(this.rect.X + this.rect.Width - this.radiu, this.rect.Y, this.radiu, this.radiu, 270, 90);

                            path.AddLine(this.rect.X + this.rect.Width, this.rect.Y + this.radiu, this.rect.X + this.rect.Width, this.rect.Y + this.rect.Height - this.radiu);

                            path.AddArc(this.rect.X + this.rect.Width - this.radiu, this.rect.Y + this.rect.Height - this.radiu, this.radiu, this.radiu, 0, 90);

                            path.AddLine(this.rect.X + this.rect.Width - this.radiu, this.rect.Y + this.rect.Height, this.rect.X + this.radiu, this.rect.Y + this.rect.Height);

                            path.AddArc(this.rect.X, this.rect.Y + this.rect.Height - this.radiu, this.radiu, this.radiu, 90, 90);

                            g.FillPath(bru_Background, path);
                        }
                    }
                    else
                    {
                        g.FillRectangle(bru_Background, (int)this.rect.X, (int)this.rect.Y, (int)this.rect.Width, (int)this.rect.Height);
                    }
                }
            }

            Point p = new Point((int)(this.rect.X + checkSize.Width / 2 - 1), (int)(this.rect.Y + (this.rect.Height - checkSize.Height) / 2));

            var state = check ?
                (this.isMouseHover ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedHot : System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal) :
                (this.isMouseHover ? System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedHot : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);


            CheckBoxRenderer.DrawCheckBox(g, p, new Rectangle((int)this.rect.X + checkSize.Width / 2 + 4, (int)this.rect.Y, (int)this.rect.Width, (int)this.rect.Height),
                     this.text, this.font, false, state);
        }

        public void OnMouseDown(MouseEventArgs e)
        {
            this.Checked = !this.check;

            Console.WriteLine(this.name + "\t" + this.text + "\n");

            if (MouseClick != null)
            {
                MouseClick(this, e);
            }

            this.parent.Refresh();
        }

        public void OnMouseEnter(MouseEventArgs e)
        {

        }


        public void OnMouseMove(MouseEventArgs e)
        {
        
        }

        public void OnMouseLeave(MouseEventArgs e)
        {
            
        }
    }
}
